package io.renren.service.impl;

import io.renren.controller.AbstractController;
import io.renren.dao.*;
import io.renren.entity.*;
import io.renren.service.ActFlowService;
import io.renren.service.SysUploadService;
import io.renren.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;

import io.renren.service.DriveExpenseReimbursementService;
import org.springframework.transaction.annotation.Transactional;


@Service("driveExpenseReimbursementService")
public class DriveExpenseReimbursementServiceImpl extends AbstractController implements DriveExpenseReimbursementService {
	@Autowired
	private DriveExpenseReimbursementDao driveExpenseReimbursementDao;
	@Autowired
	private ActAssignDao actAssignDao;
	@Autowired
	private ActFlowService actFlowService;
	@Autowired
	private SysUploadService sysUploadService;
	@Override
	public DriveExpenseReimbursementEntity queryObject(Integer id){
		return driveExpenseReimbursementDao.queryObject(id);
	}
	
	@Override
	public List<DriveExpenseReimbursementEntity> queryList(Map<String, Object> map){
		return driveExpenseReimbursementDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return driveExpenseReimbursementDao.queryTotal(map);
	}
	
	@Override
	public void save(DriveExpenseReimbursementEntity driveExpenseReimbursement){
		driveExpenseReimbursement.setFileId(String.valueOf(new Date().getTime()));
		driveExpenseReimbursementDao.save(driveExpenseReimbursement);
	}

	@Override
	public void update(DriveExpenseReimbursementEntity driveExpenseReimbursement){
		driveExpenseReimbursementDao.update(driveExpenseReimbursement);
	}
	
	@Override
	public void delete(Integer id){
		driveExpenseReimbursementDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		for(int i=0;i<ids.length;i++) {
			DriveExpenseReimbursementEntity driveExpenseReimbursementEntity = queryObject(ids[i]);
			List<SysUploadEntity>	SysOssEntityList = sysUploadService.queryListByfileId(driveExpenseReimbursementEntity.getFileId());
			if(SysOssEntityList!=null&&SysOssEntityList.size()>0){
				for(SysUploadEntity list: SysOssEntityList){
					String url = list.getUrl();
					new File(url).delete();
				}
			}
			sysUploadService.deleteByfileId(driveExpenseReimbursementEntity.getFileId());
		}
		driveExpenseReimbursementDao.deleteBatch(ids);
	}

	@Override
	@Transactional
	public String submit(DriveExpenseReimbursementEntity entity){
		Map<String,Object> map=new HashMap();
		map.put("processName","spmc03");//数据字段中spmc03代表自驾车
		String processName="spmc03";
		List<ActAssignEntity> listActAssign=actAssignDao.queryBYName(processName);
		if(listActAssign==null||listActAssign.size()==0){
			return "请在审批配置中配置审批条件！";
		}
		ActAssignEntity actAssign=listActAssign.get(0);
		Map mp=actFlowService.submitFlow(actAssign,entity,processName);
		if(mp.get("msg")!=null){
			String msg=mp.get("msg").toString();
			if(StringUtils.isNotBlank(msg)){
				return msg;
			}
		}
		entity=(DriveExpenseReimbursementEntity)mp.get("obj");
		entity.setSubmitDate(new Date());
		entity.setSubmitPerson(getUser().getUsername());
		driveExpenseReimbursementDao.update(entity);
		return "";
	}


	@Override
	@Transactional
	public void process(DriveExpenseReimbursementEntity entity){
		Map<String, Object> map=new HashMap<>();
		map.put("piid",entity.getPiid());
		map.put("remark",entity.getRemark()==null?"":entity.getRemark());
		map.put("nextTo",entity.getNextTo());
		Object obj=actFlowService.saveFlow(map,entity,"agree","spmc03");
		entity=(DriveExpenseReimbursementEntity)obj;
		driveExpenseReimbursementDao.update(entity);

	}

	@Override
	@Transactional
	public void reject(DriveExpenseReimbursementEntity entity){

		Map<String, Object> map=new HashMap<>();
		map.put("piid",entity.getPiid());
		map.put("remark",entity.getRemark()==null?"":entity.getRemark());
		map.put("nextTo",entity.getNextTo());
		Object obj=actFlowService.saveFlow(map,entity,"reject","spmc03");
		entity=(DriveExpenseReimbursementEntity)obj;
		entity.setPistatus("10");//设置为拒绝的状态
		driveExpenseReimbursementDao.update(entity);


	}



}
